package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpEmergencyContacts;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpEmergencyContactForm;
import com.edu.hre.struts.form.pim.EmpEmergencyContactListForm;

public class EmpEmergencyContactDAOService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empEmergencyContactDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpEmergencyContactForm> getEmpEmergencyContactList(int limit, int offset, int empNumber){

		ArrayList<EmpEmergencyContactForm> listResult = new ArrayList<EmpEmergencyContactForm>();
		List<HreEmpEmergencyContacts> hreContacts = baseService.getPojos("empNumber", Compare.EQUAL, empNumber, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreEmpEmergencyContacts.class);

		if (hreContacts == null) {
			return listResult;
		}
		for (HreEmpEmergencyContacts hreCon: hreContacts) {

			try {

				EmpEmergencyContactForm con = new EmpEmergencyContactForm();
				con.setId(hreCon.getId().intValue());
				con.setEmpNumber(empNumber);
				con.setName(CommonUtils.normalizeString(hreCon.getEecName()));
				con.setRelationship(CommonUtils.normalizeString(hreCon.getEecRelationship()));
				con.setHomePhone(CommonUtils.normalizeString(hreCon.getEecHomeNo()));
				con.setMobilePhone(CommonUtils.normalizeString(hreCon.getEecMobileNo()));
				con.setWorkPhone(CommonUtils.normalizeString(hreCon.getEecOfficeNo()));

				listResult.add(con);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}

		return listResult;
	}

	public int countEmpEmergencyContacts(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpEmergencyContacts.class);
		return count < 0 ? 0 : count;
	}

	public void deleteEmergencyContacts(EmpEmergencyContactListForm form){

		ArrayList<EmpEmergencyContactForm> info = (ArrayList<EmpEmergencyContactForm>) form.getAllContact();

		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpEmergencyContactForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreEmpEmergencyContacts.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
